//
//  CXTabBar.swift
//  Sweibo
//
//  Created by 陈旭 on 16/6/25.
//  Copyright © 2016年 陈旭. All rights reserved.
//

import UIKit
//声明协议
protocol CXTabBarDelegale:NSObjectProtocol{
//声明协议方法
    func composeButtonClick()
}

class CXTabBar: UITabBar {

    //声明代理
    weak var CXDelegate:CXTabBarDelegale?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = ScreenW / 5
        var index:CGFloat = 0
        
        for value in self.subviews{
        
            if
                value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                    //设置宽度
                    value.frame.size.width = width
                    value.frame.origin.x = index * width
                    
                    index++
                    if index == 2{
                    index++
                    }
            }
        }
        // 设置撰写按钮frame
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }
    //设置视图
    func setupUI(){
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
   //监听方法
    @objc private func buttonClick(){
    
        CXDelegate?.composeButtonClick()
    }
    
    //撰写按钮
    private lazy var composeButton: UIButton = {
    
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action:"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        return button
        
    }()
}

